var vm = new Vue({
    el: '#app',
    data: {
        ////////////////////////关键变量///////////////////////
        pageTotal: 10,
        //当前页
        currentPage: 1,
        //总数
        total: null,
        //一页显示的数量
        size: 10,
        //选中的行的ids
        selectColumnIds: [],
        //选中的对象
        selectColumnObjects: [],
        showFrom: false,
        dialogFormVisible: false,
        labelPosition: "right",
        //数据图对象
        option: {},
        showData: {},
        //查询参数
        params: {keyword: ""},
        ////////////////////////关键变量//////////////////////
        tableData: Array(20).fill({
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
        }),
        dialogVisible: false,
        // tableData: [],
        sysMenu: {},
        showList: true,
        title: null,
        defaultProps: {
            children: 'list',
            label: 'label'
        },
        typeItem: [{"name": "目录", "value": 0}, {"name": "菜单", "value": 1}, {"name": "按钮", "value": 2}],
        defaultProps2: {
            children: 'list',
            label: 'label',
            value: 'menuId',
        },

        filterText: '',
        allMenu: [],
        rules: {
            menuId: [
                {required: true, message: '${column.comment}不能为空', trigger: 'blur'},
                {min: 1, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
            ]
        },

    },
    methods: {
        add: function () {
            vm.sysMenu = {};
            vm.dialogFormVisible = true;
        },
        del: function () {

            vm.deleteBatch(vm.sysMenu.menuId);
        },
        saveOrUpdate: function (formName) {


            vm.dialogFormVisible = false;
            let url;
            if (vm.sysMenu.menuId != null) {
                url = "../sys/menu/update"
            } else {
                if (vm.sysMenu.parentId) {
                    vm.sysMenu.parentId=vm.sysMenu.parentId[vm.sysMenu.parentId.length-1]
                }else {
                    vm.sysMenu.parentId=0;

                }
                url = "../sys/menu/save"

            }
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.sysMenu),
                contentType: "application/json",
                type: 'POST',
                successCallback: function () {
                    vm.$message({
                        type: 'info',
                        message: '操作成功'
                    });
                }
            });

            vm.reload();
        },
        handleChange: function (val) {
            console.log(val);

        },
        getCurrentNode: function (val, val2, val3) {
            vm.sysMenu = val;
            console.log(val);
            console.log(val2);
            console.log(val3);
        },
        getAllMenu: function () {
            let _allMen;
            //sys/menu
            Ajax.request({
                url: "../sys/menu/user",
                type: 'get',
                successCallback: function (r) {
                    _allMen = r.data;
                    // vm.allMenu = r.data;
                }
            });
            this.allMenu = _allMen;

        },
        deleteBatch: function (id) {
            let ids;
            if (typeof id == "number") {
                ids = [id];
            } else {
                ids = vm.getSelectIds();
            }

            this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                Ajax.request({
                    url: "../sys/menu/delete",
                    params: JSON.stringify(ids),
                    contentType: "application/json",
                    type: 'POST',
                    successCallback: function () {
                    }
                });
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                vm.reload();
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            })
        },

        getInfo: function (id) {
            Ajax.request({
                url: "../sys/sysMenu/info/" + id,
                type: 'get',
                successCallback: function (r) {
                    vm.sysMenu = r.data;
                }
            });
        },
        handleEdit: function (index, row) {
            console.log(row);
            vm.getInfo(row.id);
            vm.dialogFormVisible = true;
        },
        handleDelete: function (id, row) {
            vm.deleteBatch(id);
        },
        filterNode(value, data) {
            console.log(value)
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },


/////////////////关键方法（无须改动）////////////////////
        query: function () {
            vm.reload();
        },
        reset: function () {
            vm.params.keyword = "";
            vm.reload();
        },
        resetForm(formName) {
            //生成后需要放开注释
            vm.dialogFormVisible = false;
        },
        ruleFrom: function (formName) {
            //生成后需要放开注释
            vm.$refs[formName].validate((valid) => {
                if (valid) {
                    this.saveOrUpdate();
                } else {
                    console.log('error submit!!');
                    return;
                }
            });
        },
        reload: function () {
            this.getAllMenu();
        },
        /**
         * 获取当前行对象
         * @returns {*}
         */
        getOneObject() {
            if (vm.selectColumnObjects.length !== 1) {
                this.$message('请选中一条记录');
                return
            }
            return vm.selectColumnObjects[0];
        },
        /**
         * 获取当前行id
         * @returns {*}
         */
        getOneId() {
            if (vm.selectColumnObjects.length !== 1) {
                this.$message('请选中一条记录');
                return
            }
            return vm.selectColumnObjects[0].id;
        },
        /**
         * 获取选中行的对象
         * @param rows
         */
        selectionRowsChange: function (rows) {
            vm.selectColumnObjects = rows;
            if (rows) {
                vm.selectColumnIds = rows.map(row => {
                    return row.id;
                })
            }
            return vm.selectColumnObjects;
        },
        /**
         * 获取选中行的ids
         */
        getSelectIds: function () {
            if (vm.selectColumnIds.length < 1) {
                this.$message('请选择');
                throw '请选择'
                // return
            }
            return vm.selectColumnIds;
        },
        /**
         * 页数更换
         * @param size
         */
        handleSizeChange: function (size) {
            vm.size = size;
        },
        /**
         * 点击行时触发
         * @param row
         * @param event
         * @param column
         */
        handleRowChange: function (row, event, column) {
            if (row) {
                this.$refs.multipleTable.toggleRowSelection(row);
            } else {
                this.$refs.multipleTable.clearSelection();
            }
        },
        /**
         * 页数更换
         * @param page
         */
        currentChange: function (page) {
            vm.currentPage = page;
        },
        /**
         * 获取列表数据
         */
        getData: function () {
            this.getAllMenu()
        },
        getShowData: function () {
            let _showData;
            Ajax.request({
                url: "../sys/sysMenu/data",
                successCallback: function (r) {
                    _showData = r.data;
                }
            });
            this.showData = _showData;
        }
/////////////////////关键方法（无须改动）/////////////
    },
    mounted: function () {
        // this.getData();
        this.getAllMenu();
        // this.getShowData();
        // this.echartsInfo();
    },
    watch: {
        currentPage(val) {
            this.getData();
        },
        limit() {
            this.getData();
        },
        filterText(val) {
            console.log(val);
            this.$refs.tree.filter(val);
        },
    }
});